package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Employee;
import biz.EmployeeBiz;
import biz.impl.EmployeeBizImpl;

public class EmployeeAction extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}



	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws UnsupportedEncodingException {
		HttpSession session = request.getSession(false);
		if (session == null) {
			return "login.jsp";
		} else {
			request.setCharacterEncoding("gbk");
			String name = request.getParameter("username");
			String password = request.getParameter("password");
			EmployeeBiz empbiz = new EmployeeBizImpl();
			Employee employee = empbiz.login(name, password);
			if (employee == null) {
				request.setAttribute("message", "�û�����������");
				return "login.jsp";
			}
			return "index.jsp";
		}
	}

	private String username;
	private String password;
	private String createTime;
	private String depName;

	public String add(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		HttpSession session = request.getSession(false);
		if (session == null)
			return "login.jsp";

		request.setCharacterEncoding("gbk");
		username = request.getParameter(username);
		password = request.getParameter(password);
		createTime = request.getParameter(createTime);
		depName = request.getParameter(depName);
		Employee employee = new Employee();
		// employee.setCreatetime(createTime);
		// employee.setDepname(depName);
		employee.setPassword(password);
		employee.setUsername(username);
		EmployeeBiz empbiz = new EmployeeBizImpl();
		boolean success = empbiz.addEmployee(employee);
		if (success == true)
			return "/employee_list.jsp";
		else
			return "/addemployee.jsp";

	}

	private String userName;
	private String passWord;
	private String CreateTime;
	private String depname;

	public String update(HttpServletRequest request,
			HttpServletResponse response) throws UnsupportedEncodingException {
		HttpSession session = request.getSession(false);
		if (session == null)
			return "/login.jsp";

		request.setCharacterEncoding("gbk");
		username = request.getParameter(username);
		password = request.getParameter(password);
		createTime = request.getParameter(createTime);
		depName = request.getParameter(depName);
		Employee employee = new Employee();
		// employee.setCreatetime(createTime);
		// employee.setDepname(depName);
		employee.setPassword(password);
		employee.setUsername(username);
		EmployeeBiz empbiz = new EmployeeBizImpl();
		boolean success = empbiz.updateEmployee(employee);
		if (success == true)
			return "/index.jsp";
		else
			return "/updateemployee.jsp";

	}
}
